#!/bin/sh

input=$1
name=$2

copyBeforMake()
{

    if [ ${name} = "V1" ];then
        echo "build bin for board Versin 1.0"
        cp /home/song/work/source/pkg/MT7620a_BoradV1/*.dts* /home/song/work/source/openwrt15.05/openwrt/target/linux/ramips/dts/
        cp /home/song/work/source/pkg/MT7620a_BoradV1/.config /home/song/work/source/openwrt15.05/openwrt/
    elif [ ${name} = "V2" ];then
        echo "build bin for board Versin 2.0"
        cp /home/song/work/source/pkg/MT7620a_BoradV2/*.dts* /home/song/work/source/openwrt15.05/openwrt/target/linux/ramips/dts/
        cp /home/song/work/source/pkg/MT7620a_BoradV2/.config /home/song/work/source/openwrt15.05/openwrt/
    else
        echo "input error"
        exit 2
    fi
}

MakeAll()
{
	make V=s -j4 && echo "make ok!"
}


copyAfterMake()
{
    echo "copy!"
    cp -rf /home/song/work/source/pkg/lib/* /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/root-ramips/usr/lib/
    cp -rf /home/song/work/source/pkg/etc/* /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/root-ramips/etc/
    #cp -rf /home/song/work/source/pkg/config/* /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/root-ramips/etc/config/
    cp -rf /home/song/work/source/pkg/bin/* /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/root-ramips/bin/
    cp -rf /home/song/work/source/pkg/other/platform.sh /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/root-ramips/lib/upgrade/
    cp /home/song/work/source/pkg/sh/*.sh /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/root-ramips/usr/sbin/

    if [ ${name} = "V2" ];then
        cp /home/song/work/source/pkg/modules/*.ko /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/root-ramips/lib/modules/3.18.29/
        cp -rf /home/song/work/source/pkg/config/userV2 /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/root-ramips/etc/config/user
    else
        cp -rf /home/song/work/source/pkg/config/userV1 /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/root-ramips/etc/config/user
    fi

    #cp /home/song/work/source/eclipse/ser485net/Debug/ser485net /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/root-ramips/usr/bin/

    cp /home/song/work/source/eclipse/heartbeat/Debug/heartbeat /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/root-ramips/usr/bin/

}

remake()
{
#make...
    /home/song/work/source/openwrt15.05/openwrt/staging_dir/host/bin/mksquashfs4 /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/root-ramips /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620/root.squashfs -nopad -noappend -root-owned -comp xz -Xpreset 9 -Xe -Xlc 0 -Xlp 2 -Xpb 2  -b 256k -p '/dev d 755 0 0' -p '/dev/console c 600 0 0 5 1' -processors 1

    /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/OpenWrt-SDK-ramips-mt7620_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-x86_64/staging_dir/host/bin/mkimage -A mips -O linux -T kernel -C lzma -a 0x80000000 -e 0x80000000 -n "MIPS OpenWrt Linux-3.18.29" -d /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620/vmlinux-mt7620a.bin.lzma /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620/vmlinux-mt7620a.uImage

    cat /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620/vmlinux-mt7620a.uImage /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620/root.squashfs > /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620/openwrt-ramips-mt7620-mt7620a-squashfs-sysupgrade.bin

    /home/song/work/source/openwrt15.05/openwrt/staging_dir/host/bin/padjffs2 /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620/openwrt-ramips-mt7620-mt7620a-squashfs-sysupgrade.bin 4 8 16 64 128 256

    if [ `stat -c%s "/home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620/openwrt-ramips-mt7620-mt7620a-squashfs-sysupgrade.bin"` -gt 16121856 ]; then 
        echo "Warning: /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620/openwrt-ramips-mt7620-mt7620a-squashfs-sysupgrade.bin is too big" >&2; 
	fi
        f=`date "+%Y%m%d"`
    if [ ${name} = "V1"  ];then
        cp -fpR /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620/openwrt-ramips-mt7620-mt7620a-squashfs-sysupgrade.bin /home/song/work/source/out_bin/LteNetworkDev-V1.0_${f}.bin;
        echo "makebin is ok! the DIR:/home/song/work/source/out_bin/LteNetworkDev-V1.0_${f}.bin\n"
    else
        cp -fpR /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620/openwrt-ramips-mt7620-mt7620a-squashfs-sysupgrade.bin /home/song/work/source/out_bin/LteNetworkDev-V2.0_${f}.bin; 
        echo "makebin is ok! the DIR:/home/song/work/source/out_bin/LteNetworkDev-V2.0_${f}.bin\n"
    fi

}


if [ ${input} = bin ];then
    rm /home/song/work/source/openwrt15.05/openwrt/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620/openwrt-ramips-mt7620-mt7620a-squashfs-sysupgrade.bin
	copyBeforMake
	MakeAll
	copyAfterMake
	remake
elif [ ${input} = pkg ];then
	copyAfterMake
	remake
elif [ ${input} = compile ];then
        make package/${name}/compile V=s
elif [ ${input} = mkpkg  ];then
        make package/${name}/compile V=s
        make package/${name}/install V=s
        make package/index V=s   
fi
